package fellipe.exemplo.twittertes;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class TelaLogin extends Activity {

	private static final String USER = "admin";
	private static final String PASS = "admin";
	
	private Button botaoLogin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.telalogin);
		
		SharedPreferences sp = getSharedPreferences("login_usuario", Activity.MODE_PRIVATE);
		if (!sp.getString("user", "").equals("")) {
			Intent i = new Intent(TelaLogin.this, MainActivity.class);
			startActivity(i);
			finish();
		}
		
		botaoLogin = (Button) findViewById(R.id.buttonlogin);
		botaoLogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String user = ((EditText) findViewById(R.id.login_usuario)).getText().toString();
				String pass = ((EditText) findViewById(R.id.senha_usuario)).getText().toString();
				
				if (user.equals(USER) && pass.equals(PASS)) {;
					SharedPreferences sp = getSharedPreferences("login_usuario", Activity.MODE_PRIVATE);
					SharedPreferences.Editor ed = sp.edit();
					ed.putString("user", user);
					ed.putString("pass", pass);
					ed.commit();
					
					Intent i = new Intent(TelaLogin.this, MainActivity.class);
					startActivity(i);
					finish();
				}
			}
		});
	}
	
}
